#include "ObjModel.hpp"
#include "ObjFileLoader.hpp"

ObjModel::ObjModel()
{
}

ObjModel::ObjModel(Shader& shader): Model(shader)
{
}

ObjModel::ObjModel(ObjModel const& copy): Model(copy)
{
}

ObjModel::~ObjModel(void)
{
}

void	ObjModel::load(std::string const& modelFile, const bool calcNormal) throw (std::exception)
{
	ObjFileLoader	loader;

    _path = modelFile;
	loader.load(modelFile, calcNormal);
	if (loader.isModelLoaded())
	{
		_nbVertices = loader.getNbVertices();
		_vertices = loader.getVertices();
        _index = NULL;
        _nbIndex = 0;

		init();
	}
}

void	ObjModel::init(void)
{
	// Create VBO
	glGenBuffers(VBO_SIZE, _vbo);
	
	// Init VBO for vertex
	glBindBuffer(GL_ARRAY_BUFFER, _vbo[VBO_VERTEX]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(VertexAttributes) * _nbVertices, _vertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void	ObjModel::begin(void)
{
    glBindBuffer(GL_ARRAY_BUFFER, _vbo[VBO_VERTEX]);
    shaderAttributesGet();
    shaderAttributesEnable();
    shaderAttributesSendData();
    _shader->sendUniformData("UniObjectPosition", _position);
    _shader->sendUniformData("TextureEnable", -1.f);

}

void	ObjModel::draw(void)
{
	begin();
	glDrawArrays(GL_TRIANGLES, 0, _nbVertices);
	end();
}

void	ObjModel::end(void)
{
    shaderAttributesDisable();
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
